package com.superman.supermarket.client.view.casherdesk;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.widget.toggle.ToggleItem;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent;
import com.superman.mvpframe.client.widget.toggle.ToggleSelectEvent.ToggleSelectEventHandler;
import com.superman.supermarket.client.widget.bottombutton.BottomButton;
import com.superman.supermarket.client.widget.payment.IPayment;
import com.superman.supermarket.client.widget.payment.PaymentChoose;
import com.superman.supermarket.client.widget.payment.PaymentType;

/**
 * 收银台页面工具类
 * 
 * @author superman
 * @version 2018年8月12日下午3:43:01
 */
public class CasherDeskViewImpl extends Composite implements CasherDeskView {

	interface CasherDeskViewImplUiBinder extends UiBinder<Widget, CasherDeskViewImpl> {
	}

	private static CasherDeskViewImplUiBinder uiBinder = GWT.create(CasherDeskViewImplUiBinder.class);

	private boolean pay;
	@UiField
	BottomButton paybtn;
	@UiField
	PaymentChoose paymentchoose;
	@UiField
	HTML tipshtml, paytipshtml;
	@UiField
	FlowPanel paypanel;

	private String payType;
	private CasherDeskViewPresenter presenter;

	@UiField
	HTML pricehtml;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午3:43:03
	 */
	public CasherDeskViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		paymentchoose.addToggleSelectEventHandler(new ToggleSelectEventHandler() {

			@Override
			public void onSelected(ToggleSelectEvent event) {
				ToggleItem toggleItem = event.getToggleItem();
				if (toggleItem instanceof IPayment) {
					IPayment payment = (IPayment) toggleItem;
					payType = payment.getType();
				}
			}
		});
	}

	@Override
	public String getPaymentType() {
		return payType;
	}

	@Override
	public void init() {
		List<String> paymentList = new ArrayList<String>();
		paymentList.add(PaymentType.ALIPAY.getType());
		paymentList.add(PaymentType.WXPAY.getType());
		paymentchoose.setSupportPaymentList(paymentList);
	}

	@UiHandler("paybtn")
	void pay(ClickEvent event) {
		if (pay)
			presenter.pay();
		else
			presenter.pushToOrderDetail();
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (CasherDeskViewPresenter) presenter;
	}

	@Override
	public void setPayPrice(double price) {
		pricehtml.setHTML("¥&nbsp;" + price);
	}

	@Override
	public void setSubmitBtn(boolean pay) {
		this.pay = pay;
		if (pay) {
			tipshtml.setText("订单已提交");
			paybtn.setText("立即支付");
			paytipshtml.setVisible(true);
			paymentchoose.setVisible(true);
		} else {
			tipshtml.setText("支付成功");
			paybtn.setText("查看订单");
			paytipshtml.setVisible(false);
			paymentchoose.setVisible(false);
		}
	}

	@Override
	public void setPayInfo(Map<String, Object> map) {
		HTML orderidHTML = new HTML();
		orderidHTML.setHTML("订单编号:&nbsp;&nbsp;" + String.valueOf(map.get("ORDER_ID")));
		paypanel.add(orderidHTML);

		HTML paytimeHTML = new HTML();
		paytimeHTML.setHTML("支付时间:&nbsp;&nbsp;" + String.valueOf(map.get("PAYMENT_TIME")));
		paypanel.add(paytimeHTML);

		for (int i = 0; i < PaymentType.values().length; i++) {
			PaymentType paymentType = PaymentType.values()[i];
			if (paymentType.getType().equals(String.valueOf(map.get("PAYMENT")))) {
				HTML paytypeHTML = new HTML();
				paytypeHTML.setHTML("支付方式:&nbsp;&nbsp;" + paymentType.getTypeName());
				paypanel.add(paytypeHTML);
				return;
			}
		}

	}

}
